home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Lines Curves and Area Fills / HelloPrinter / HelloPrinter.cs next >
Encoding:
Text File  |  2001-01-15  |  1.6 KB  |  52 lines

  1. //-------------------------------------------
  2. // HelloPrinter.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8.  
  9. class HelloPrinter: Form
  10. {
  11.      public static void Main()
  12.      {
  13.           Application.Run(new HelloPrinter());
  14.      }
  15.      public HelloPrinter()
  16.      {
  17.           Text = "Hello Printer!";
  18.           BackColor = SystemColors.Window;
  19.           ForeColor = SystemColors.WindowText;
  20.      }
  21.      protected override void OnPaint(PaintEventArgs pea)
  22.      {
  23.           Graphics     grfx   = pea.Graphics;
  24.           StringFormat strfmt = new StringFormat();
  25.  
  26.           strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
  27.  
  28.           grfx.DrawString("Click to print", Font, new SolidBrush(ForeColor),
  29.                           ClientRectangle, strfmt);
  30.      }
  31.      protected override void OnClick(EventArgs ea)
  32.      {
  33.           PrintDocument prndoc = new PrintDocument();
  34.  
  35.           prndoc.DocumentName = Text;
  36.           prndoc.PrintPage +=
  37.                  new PrintPageEventHandler(PrintDocumentOnPrintPage);
  38.           prndoc.Print();
  39.      }
  40.      void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
  41.      {
  42.           Graphics grfx = ppea.Graphics;
  43.  
  44.           grfx.DrawString(Text, Font, Brushes.Black, 0, 0);
  45.  
  46.           SizeF sizef = grfx.MeasureString(Text, Font);
  47.  
  48.           grfx.DrawLine(Pens.Black, sizef.ToPointF(), 
  49.                                     grfx.VisibleClipBounds.Size.ToPointF());
  50.      }
  51. }
  52.